<template>
<div class="pb50">
    <el-form size="small" ref="form" :model="form" label-width="100px">
      <el-form-item :label="$t('plus.enable_home_push')" prop="is_open">
        <el-switch v-model="form.isOpen"></el-switch>
      </el-form-item>
      <el-form-item
        :label="$t('plus.activity_name')"
        :rules="[{  required: true,  message: ' '}]"
        prop="name"
      >
        <el-input
          class="max-w460"
          v-model="form.name"
          placeholder=""
          type="text"
        ></el-input>
        <el-col class="gray"
          >{{$t('plus.suggest_change_name')}}</el-col
        >
      </el-form-item>
      <div v-if="form.isOpen">
        <el-form-item :label="$t('plus.type')">
          <el-radio v-model="form.type" label="1">{{$t('plus.image_text')}}</el-radio>
          <el-radio v-model="form.type" label="2">{{$t('plus.pure_image')}}</el-radio>
          <el-radio v-model="form.type" label="3">{{$t('plus.receive_coupon')}}</el-radio>
        </el-form-item>

        <el-form-item
          v-if="form.type == 1"
          :label="$t('plus.title')"
          :rules="[{  required: true,  message: ' '}]"
          prop="title"
        >
          <el-input
            v-model="form.title"
            :placeholder="$t('plus.input_title')"
            class="max-w460"
          ></el-input>
        </el-form-item>

        <el-form-item :label="$t('plus.popup_image')">
          <el-row>
            <el-button icon="Upload" @click="openUpload">{{$t('plus.choose_image')}}</el-button>
            <div v-if="form.imageId > 0" class="img mt10">
              <img :src="form.filePath" width="100" height="100" />
            </div>
          </el-row>
          <el-col class="gray"
            >{{$t('plus.image_limit_tip')}} 
<span v-if="form.type == 1">{{$t('plus.image_size_600x300')}}</span>
            <span v-if="form.type == 2">{{$t('plus.image_size_600x420')}}</span>
            <span v-if="form.type == 3">{{$t('plus.image_size_600x250')}}</span>
          </el-col>
        </el-form-item>
        <el-form-item
          v-if="form.type == 1"
          :label="$t('plus.button_text_label')"
          :rules="[{  required: true,  message: ' '}]"
          prop="des"
        >
          <el-input
            class="max-w460"
            v-model="form.des"
            placeholder=""
            type="text"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="form.type == 1"
          :label="$t('plus.description')"
          :rules="[{  required: true,  message: ' '}]"
          prop="remark"
        >
          <el-input
            type="textarea"
            :rows="2"
            max="100"
            :placeholder="$t('plus.input_description')"
            v-model="form.remark"
            class="max-w460"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          :label="$t('plus.jump_url')"
          v-if="form.type < 3"
          :rules="[{  required: true,  message: ' '}]"
          prop="link"
        >
          <el-input
            class="max-w460"
            :placeholder="$t('plus.click_select_link')"
            v-if="form.link"
            :value="$t('plus.link_to') + form.link.type + ' ' + form.link.name"
            :disabled="true"
          ></el-input>
          <el-input
            class="max-w460"
            :placeholder="$t('plus.click_select_link')"
            v-if="!form.link"
            :disabled="true"
          ></el-input>
          <el-button icon="Link" @click="changeLink">{{$t('plus.select_link')}}</el-button>
        </el-form-item>
        <!--选择优惠券-->
        <el-form-item :label="$t('plus.select_coupon')" v-if="form.type == 3">
          <el-button size="small" icon="Plus" @click="addCoupon()"
            >{{$t('plus.add_coupon')}}</el-button
          > {{$t('plus.max_three_note')}} 

</el-form-item>
        <el-form-item v-if="form.type == 3">
          <div class="flex-1">
            <el-table :data="coupon" style="width: 100%">
              <el-table-column prop="name" :label="$t('plus.coupon')">
                <template #default="scope">
                  <span class="fb orange">{{scope.row.name}}</span>
                </template>
              </el-table-column>
              <el-table-column prop="name" :label="$t('plus.type')">
                <template #default="scope">
                  <span class="fb orange" v-if="scope.row.type == 10"
                    >{{$t('plus.coupon_full_reduction')}}</span
                  >
                  <span class="fb orange" v-if="scope.row.type == 20"
                    >{{$t('plus.coupon_discount')}}</span
                  >
                </template>
              </el-table-column>
              <el-table-column :label="$t('plus.action')" width="100">
                <template #default="scope">
                  <el-button
                    @click="deleteCouponClick(scope.$index)"
                    type="text"
                    size="small"
                    >{{$t('plus.delete')}}</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-form-item>
      </div>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button
          size="small"
          type="primary"
          @click="onSubmit"
          :loading="loading"
          >{{$t('plus.submit')}}</el-button
        >
      </div>
    </el-form>

    <!--上传图片组件-->
    <Upload
      v-if="isupload"
      :isupload="isupload"
      :type="type"
      @returnImgs="returnImgsFunc"
      >{{$t('plus.upload_image_button')}}</Upload
    >

    <!--选择连接-->
    <Setlink
      v-if="is_linkset"
      :is_linkset="is_linkset"
      @closeDialog="closeLinkset"
      >{{$t('plus.select_link')}}</Setlink
    >

    <!--选择优惠券-->
    <GetCoupon
      v-if="open_add"
      :open_add="open_add"
      @closeDialog="closeProductDialogFunc($event)"
      >{{$t('plus.select_coupon_popup')}}</GetCoupon
    >
  </div>
</template>

<script>
import HomepushApi from "@/api/homepush.js";
import Upload from "@/components/file/Upload.vue";
import Setlink from "@/components/setlink/Setlink.vue";
import GetCoupon from "@/components/coupon/GetCoupon.vue";
export default {
  components: {
    Upload,
    Setlink,
    GetCoupon
  },
  data() {
    return {
      form: {
        isOpen: false,
        name: "",
        des: $t("plus.view_details"),
        title: "",
        remark: "",
        link: "",
        type: "1",
        imageId: 0,
        filePath: ""
      },
      coupon: [],
      /*是否加载完成*/
      loading: false,
      /*添加优惠券*/
      open_add: false,
      /*是否上传图片*/
      isupload: false,
      /*是否选择链接*/
      is_linkset: false
    };
  },
  created() {
    this.getData();
  },
  methods: {
    /*上传*/
    openUpload(e) {
      this.type = e;
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      if (e != null && e.length > 0) {
        this.form.filePath = e[0].filePath;
        this.form.imageId = e[0].fileId;
      }
      this.isupload = false;
    },
    /*获取数据*/
    getData() {
      let self = this;
      HomepushApi.getData().then(res => {
        self.form = res.data;
        if (self.form.isOpen == "true") {
          self.form.isOpen = true;
        }
        if (!self.form.hasOwnProperty("coupon")) {
          self.coupon = [];
        } else {
          self.coupon = self.form.coupon;
        }
      }).catch(error => {});
    },
    /*提交*/
    onSubmit() {
      let self = this;
      let params = self.form;
      if (self.form.type == 3) {
        params.coupon = self.coupon;
      }
      if (params.is_open && !self.checkData(params)) {
        return false;
      }
      self.loading = true;
      HomepushApi.saveData(params, true).then(data => {
        self.loading = false;
        if (data.code == 1) {
          ElMessage({
            message: $t("plus.save_success"),
            type: "success"
          });
          self.getData();
        }
      }).catch(error => {
        self.loading = false;
      });
    },
    /*选择链接*/
    changeLink() {
      this.is_linkset = true;
    },
    /*获取链接并关闭弹窗*/
    closeLinkset(e) {
      this.is_linkset = false;
      if (e) {
        this.form.link = e;
      }
    },
    /*添加优惠券*/
    addCoupon() {
      this.open_add = true;
    },
    /*关闭弹窗*/
    closeProductDialogFunc(e) {
      this.open_add = e.openDialog;
      if (e.type == "success") {
        let len = this.coupon.length;
        if (len >= 3) {
          ElMessage({
            message: $t("plus.max_three_coupons"),
            type: "error"
          });
          return false;
        }
        let obj = {};
        obj.couponId = e.params.couponId;
        obj.name = e.params.name;
        obj.type = e.params.couponType;
        this.coupon.push(obj);
      }
    },
    /*删除优惠券*/
    deleteCouponClick(e) {
      this.coupon.splice(e, 1);
    },
    /*数据验证*/
    checkData(param) {
      if (param.type == 1) {
        if (!param.title) {
          ElMessage({
            message: $t("plus.title_required"),
            type: "error"
          });
          return false;
        }
        if (!param.des || !param.remark) {
          return false;
        }
      }
      if (param.type < 3) {
        if (!param.link) {
          ElMessage({
            message: $t("plus.link_required"),
            type: "error"
          });
          return false;
        }
      } else {
        if (param.coupon.length == 0) {
          ElMessage({
            message: $t("plus.coupon_required"),
            type: "error"
          });
          return false;
        }
      }
      if (!(param.image_id > 0)) {
        ElMessage({
          message: $t("plus.upload_image"),
          type: "error"
        });
        return false;
      }
      return true;
    }
  }
};
</script>

<style>
.reward-list .reward-item {
  margin-bottom: 10px;
  padding: 10px 20px;
  border: 1px solid #ebeef5;
}

.reward-list .delete-reward {
  position: absolute;
  top: 10px;
  right: 20px;
  cursor: pointer;
  z-index: 10;
}
</style>

